package com.behome.stream.controller;

import com.behome.stream.common.domain.Result;
import com.behome.stream.domain.TSysParam;
import com.behome.stream.service.TSysParamService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * SystemParamController 系统配置控制器
 *
 * @author bh_ljy
 * @version 2024/06/05 17:59
 **/
@RestController
@RequestMapping("/systemParam")
public class SystemParamController {
    @Resource
    private TSysParamService systemParamService;

    @GetMapping("/listData")
    public Result listData(int page, int limit) {
        List<TSysParam> list = systemParamService.listData();
        int totalCount = list.size();
        List<TSysParam> pageList = new ArrayList<>();
        //计算当前页第一条数据的下标
        int currId = page > 1 ? (page - 1) * limit : 0;
        for (int i = 0; i < limit && i < totalCount - currId; i++) {
            pageList.add(list.get(currId + i));
        }
        return Result.succeed("获取系统参数成功!", totalCount, pageList);
    }

    @PostMapping("/add")
    public Result add(@RequestBody TSysParam sysParam) {
        int insert = systemParamService.insert(sysParam);
        return insert == 1 ? Result.succeed("添加成功") : Result.fail(-1, "添加失败");
    }

    @PutMapping()
    public Result edit(@RequestBody TSysParam sysParam) {
        int insert = systemParamService.update(sysParam);
        return insert == 1 ? Result.succeed("修改成功") : Result.fail(-1, "修改失败");
    }

    @DeleteMapping("/{Id}")
    public Result remove(@PathVariable("Id") Long id) {
        int insert = systemParamService.delete(id);
        return insert == 1 ? Result.succeed("删除成功") : Result.fail(-1, "删除失败");
    }

}
